package com.chinags.system.report.service.impl;

import java.util.Date;
import java.util.List;

import com.chinags.common.exception.BusinessException;
import com.chinags.common.utils.StringUtils;
import com.chinags.system.report.domain.TJcFlzddw;
import com.chinags.system.report.domain.TJcJcbg;
import com.chinags.system.report.mapper.TJcFlzddwMapper;
import com.chinags.system.report.mapper.TJcJcbgMapper;
import com.chinags.system.report.service.ITJcJcbgService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.chinags.common.core.text.Convert;

/**
 * 检测报告基本 服务层实现
 * 
 * @author liut
 * @date 2019-06-20
 */
@Service
public class TJcJcbgServiceImpl implements ITJcJcbgService
{
	private static final Logger log = LoggerFactory.getLogger(TJcJcbgServiceImpl.class);
	@Autowired
	private TJcJcbgMapper tJcJcbgMapper;
	@Autowired
    private TJcFlzddwMapper tJcFlzddwMapper;

	/**
     * 查询检测报告基本信息
     * 
     * @param reportId 检测报告基本ID
     * @return 检测报告基本信息
     */
    @Override
	public TJcJcbg selectTJcJcbgById(Long reportId)
	{
	    return tJcJcbgMapper.selectTJcJcbgById(reportId);
	}
	
	/**
     * 查询检测报告基本列表
     * 
     * @param tJcJcbg 检测报告基本信息
     * @return 检测报告基本集合
     */
	@Override
	public List<TJcJcbg> selectTJcJcbgList(TJcJcbg tJcJcbg)
	{
	    return tJcJcbgMapper.selectTJcJcbgList(tJcJcbg);
	}
	
    /**
     * 新增检测报告基本
     * 
     * @param tJcJcbg 检测报告基本信息
     * @return 结果
     */
	@Override
	public int insertTJcJcbg(TJcJcbg tJcJcbg)
	{
	    return tJcJcbgMapper.insertTJcJcbg(tJcJcbg);
	}
	
	/**
     * 修改检测报告基本
     * 
     * @param tJcJcbg 检测报告基本信息
     * @return 结果
     */
	@Override
	public int updateTJcJcbg(TJcJcbg tJcJcbg)
	{
	    return tJcJcbgMapper.updateTJcJcbg(tJcJcbg);
	}

	/**
     * 删除检测报告基本对象
     * 
     * @param ids 需要删除的数据ID
     * @return 结果
     */
	@Override
	public int deleteTJcJcbgByIds(String ids)
	{
		return tJcJcbgMapper.deleteTJcJcbgByIds(Convert.toStrArray(ids));
	}

	@Override
	public String importUser(List<TJcJcbg> tJcJcbgList, boolean updateSupport, String operName) {
		if (StringUtils.isNull(tJcJcbgList) || tJcJcbgList.size() == 0)
		{
			throw new BusinessException("导入用户数据不能为空！");

		}

		int successNum = 0;
		int failureNum = 0;
		StringBuilder successMsg = new StringBuilder();
		StringBuilder failureMsg = new StringBuilder();
//		String password = configService.selectConfigByKey("sys.tjcFlzddw.initPassword");
		for (TJcJcbg tJcJcbg : tJcJcbgList)
		{
			try
			{
				// 验证是否存在这个报告
				TJcJcbg t = tJcJcbgMapper.selectTjcjcbgByReportId(tJcJcbg.getReportId());
//				Long flzddwId = tJcJcbg.getFlzddwId();
//				String detectionId = tJcJcbg.getDetectionId();
//				Date detectionDate = tJcJcbg.getDetectionDate();
//                //验证报告的受检单位社会统一代码是否存在
//				if(flzddwId != null) {
//					List<Long> tJcFlzddwList = tJcFlzddwMapper.selectCredit();
//					for (Long creditCode : tJcFlzddwList) {
//						if (flzddwId == creditCode) {
//							failureNum++;
//							failureMsg.append("<br/>" + failureNum + "、受检单位社会信用代码 " + tJcJcbg.getReportName() + " 重复");
//						}
//					}
//				}
//               //验证报告的检测机构的社会统一代码是否存在
//				if(StringUtils.isNotEmpty(detectionId)) {
//					List<String> detectionIdList = tJcFlzddwMapper.selectDetection();
//					for (String detection : detectionIdList) {
//						if (detection == detectionId) {
//							failureNum++;
//							failureMsg.append("<br/>" + failureNum + "、检测机构社会信用代码 " + tJcJcbg.getReportName() + " 重复");
//						}
//					}
//				}
				if (StringUtils.isNull(t))
				{
//					String checkName = tJcJcbg.getCheckName();
//					Long code = tJcFlzddwMapper.selectCreditCodeBycheckName(checkName);

					tJcJcbg.setCreateBy(operName);
					this.insertTJcJcbg(tJcJcbg);
					successNum++;
					successMsg.append("<br/>" + successNum + "、 报告" + tJcJcbg.getReportName() + " 导入成功");
				}
				else if (updateSupport)
				{
					tJcJcbg.setUpdateBy(operName);
					this.updateTJcJcbg(tJcJcbg);
					successNum++;
					successMsg.append("<br/>" + successNum + "、 " + tJcJcbg.getReportName() + " 更新成功");
				}
				else
				{
					failureNum++;
					failureMsg.append("<br/>" + failureNum + "、单位 " + tJcJcbg.getReportName() + " 已存在");
				}
			}
			catch (Exception e)
			{
				failureNum++;
				String msg = "<br/>" + failureNum + "、单位 " + tJcJcbg.getReportName() + " 导入失败：";
				failureMsg.append(msg + e.getMessage());
				log.error(msg, e);
			}
		}
		if (failureNum > 0)
		{
			failureMsg.insert(0, "很抱歉，导入失败！共 " + failureNum + " 条数据格式不正确，错误如下：");
			throw new BusinessException(failureMsg.toString());
		}
		else
		{
			successMsg.insert(0, "恭喜您，数据已全部导入成功！共 " + successNum + " 条，数据如下：");
		}
		return successMsg.toString();

	}

	@Override
	public TJcJcbg selectJobLogById(Long reportId) {
		return tJcJcbgMapper.selectJobLogById(reportId);
	}


}
